import { BlockEnum } from '../../types'
import type { NodeDefault } from '../../types'
import { AuthorizationType, BodyType, MetaClassType, Method } from './types'
import type { BodyPayload, MetaOperatorNodeType } from './types'
import { ALL_CHAT_AVAILABLE_BLOCKS, ALL_COMPLETION_AVAILABLE_BLOCKS } from '@/components/workflow/constants'

const nodeDefault: NodeDefault<MetaOperatorNodeType> = {
  defaultValue: {
    variables: [],
    method: Method.get,
    class_type: MetaClassType.class,
    identifier: '',
    body: '',
    timeout: {
      max_connect_timeout: 0,
      max_read_timeout: 0,
      max_write_timeout: 0,
    },
  },
  getAvailablePrevNodes(isChatMode: boolean) {
    const nodes = isChatMode
      ? ALL_CHAT_AVAILABLE_BLOCKS
      : ALL_COMPLETION_AVAILABLE_BLOCKS.filter(type => type !== BlockEnum.End)
    return nodes
  },
  getAvailableNextNodes(isChatMode: boolean) {
    const nodes = isChatMode ? ALL_CHAT_AVAILABLE_BLOCKS : ALL_COMPLETION_AVAILABLE_BLOCKS
    return nodes
  },
  checkValid(payload: MetaOperatorNodeType, t: any) {
    let errorMessages = ''

    if (!errorMessages && !payload.identifier)
      errorMessages = t('workflow.errorMsg.fieldRequired', { field: t('workflow.nodes.metaOperator.api') })

    return {
      isValid: !errorMessages,
      errorMessage: errorMessages,
    }
  },
}

export default nodeDefault
